package xian.woniu.web.controller;

import com.github.pagehelper.PageInfo;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import xian.woniu.entity.Doctor;
import xian.woniu.entity.Orders;
import xian.woniu.entity.Patient;
import xian.woniu.entity.Patient;
import xian.woniu.service.IDoctorService;
import xian.woniu.service.IOrdersService;
import xian.woniu.service.IPatientService;
import xian.woniu.util.ResultVO;

import java.util.Arrays;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author SongHang
 * @time 2022/06/30 15:12:48
 */
@RestController
@RequestMapping("doctor")
public class DoctorController {
    private IDoctorService doctorService;
    private IOrdersService ordersService;
    private IPatientService patientService;
    public DoctorController(IDoctorService doctorService,IOrdersService ordersService,IPatientService patientService) {
        this.doctorService = doctorService;
        this.ordersService=ordersService;
        this.patientService=patientService;
    }

    @PostMapping("save")
    public ResultVO save(@RequestBody Doctor doctor) {
        int i = doctorService.save(doctor);
        return i > 0 ? ResultVO.success("存储医生信息成功！") : ResultVO.failure("存储医生信息失败！");
    }

    @GetMapping("delete/{did}")
    public ResultVO delete(@PathVariable String did) {
        if (StringUtils.isEmpty(did)) {
            return ResultVO.failure("删除医生信息时，id为空  " + did);
        }
        int i = doctorService.delete(Integer.valueOf(did));
        return i > 0 ? ResultVO.success("删除医生信息成功！") : ResultVO.failure("删除医生信息失败！");
    }

    @GetMapping("findOne/{did}")
    public ResultVO findOne(@PathVariable String did) {
        if (StringUtils.isEmpty(did)) {
            return ResultVO.failure("findOne医生信息时，id为空  " + did);
        }
        Doctor i = doctorService.findOne(Integer.valueOf(did));
        return i != null ? ResultVO.success("findOne医生信息成功！",i) : ResultVO.failure("findOne医生信息失败！");
    }

    @GetMapping("findAll")
    public ResultVO findAll() {
        List<Doctor> all = doctorService.findAll();
        return all != null&&all.size()>0 ? ResultVO.success("findAll医生信息成功！",all) : ResultVO.failure("findOne医生信息失败！");
    }

    @GetMapping("findAllWithText")
    public ResultVO findAllWithText() {
        List<Doctor> list = doctorService.findAllWithText();
        return list != null&&list.size()>0 ? ResultVO.success("findAll医生信息成功！",list) : ResultVO.failure("findOne医生信息失败！");
    }

    @PostMapping("update")
    public ResultVO update(@RequestBody Doctor doctor) {
        int i = doctorService.update(doctor);
        return i > 0 ? ResultVO.success("更改医生信息成功！") : ResultVO.failure("更改医生信息失败！");
    }

    /**
     * 根据uid查找医生信息
     * @param uid
     * @return
     */
    @GetMapping("getDoctorByUid/{uid}")
    public ResultVO getDoctorByUid(@PathVariable String uid) {
        if (StringUtils.isEmpty(uid)) {
            return ResultVO.failure("getDoctorByUid医生信息时，id为空  " + uid);
        }
        Doctor i = doctorService.getDoctorByUid(Integer.valueOf(uid));
        return i != null ? ResultVO.success("getDoctorByUid医生信息成功！",i) : ResultVO.failure("getDoctorByUid医生信息失败！");
    }


    @GetMapping("getwaittask")
    public ResultVO getwaittask(){
        List<Doctor> getwaittask = doctorService.getwaittask();
        if(getwaittask!=null){
            return ResultVO.success(getwaittask);
        }
        return ResultVO.success("暂无审核医生");
    }


    @GetMapping("getAllpatientInfo/{did}")
    public ResultVO getAllpatientInfo(@PathVariable("did") Integer did){
        List<Patient> patients = doctorService.showPatientInfoByDid(did);

        if(patients==null||patients.size()==0){
            return ResultVO.failure();
        }
        return ResultVO.success(patients);

    }

    /**
     * @CreateBy: hzz
     * @Description: 签约记录
     *               医生可以查看签约自己的患者记录
     *               Feign调用
     * @CreateTime: 2022/7/5 19:25
     * @Params:  * @Param: did  医生id
     * @Return:
     */
    @GetMapping("getAllOrdesByDid")
    public ResultVO getAllOrdesByDid(Integer did) throws JsonProcessingException {
        System.out.println(did);
        //通过医生id查询该医生的所有签约记录
        //get请求
        ResultVO byDid = ordersService.findByDid(did);
        Object data = byDid.getData();
        List<Orders> ordersList = JSONObject.parseArray(JSONObject.toJSONString(data)).toJavaList(Orders.class);
        //通过患者id查询患者信息
        List<Patient> list=new ArrayList();
        for(int i=0;i<ordersList.size();i++){
            //post请求
            ResultVO resultVO1 = patientService.selPatientByPid(ordersList.get(i).getPid());
            Map map=(Map)resultVO1.getData();
            Patient patient=new Patient();
            try {
                BeanUtils.populate(patient,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            ordersList.get(i).setPname(patient.getPname());
        }

        return ResultVO.success("查询成功",ordersList);
    }

    @PostMapping("getDoctorByDids")
    public ResultVO getDoctorByUids(@RequestBody Map map){
        List<Integer> did = (List<Integer>) map.get("did");
        Integer pageNow = (Integer) map.get("pageNow");
        Integer pageSize = (Integer) map.get("pageSize");
        List<Doctor> doctor = doctorService.getDoctorByDids(did, pageNow, pageSize);
        PageInfo<Doctor> pageInfo  = new PageInfo<Doctor>(doctor);
        return ResultVO.success(pageInfo);
    }
}
